from base.base import BaseTask
import requests
import json
import datetime
import os
from base.notify import send


class QiHckTask(BaseTask):

    def __init__(self, token):
        self.token = token
        return

    def get_my_coupon(self):

        # 定义请求的URL
        url = "https://xapi.weimob.com/api3/onecrm/coupon/v1/custom/queryUserCouponFlowPage"

        headers = {
            "Host": "xapi.weimob.com",
            "Cookie": "rprm_cuid=7896440526qme0vege0o",
            "Content-Length": "761",
            "X-Apm-Parent-Page-Id": "c2e00750-2939-620c-2772-aafba62bc2",
            "X-Wmsdk-Close-Store": "v2",
            "X-Component-Is": "onecrm/coupon-list",
            "Weimob-Pid": "100000559348",
            "Parentrpcid": "bd5d4d6cc859a2a4",
            "X-Page-Route": "onecrm/coupon-list",
            "X-Cmssdk-Vidticket": "18046-1707896542.253-saas-w1-1400-20752306195",
            "Wos-X-Channel": "0:TITAN",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x6309092b) XWEB/8555",
            "Content-Type": "application/json",
            "X-Wx-Token": self.token,
            "X-Wmsdk-Bc": "1 1707896440541",
            "Weimob-Bosid": "4000289554547",
            "X-Cms-Sdk-Request": "1.5.21",
            "X-Apm-Page-Id": "5007973b-9fbb-af22-c275-b7d10f965d",
            "X-Apm-Conversation-Id": "3f432809-e0a7-d5bb-ceb3-d475ac37fa",
            "X-Req-From": "onecrm",
            "Xweb_xhr": "1",
            "X-Wmsdk-Vid": "6001091846547",
            "X-Biz-Id": "146",
            "Accept": "*/*",
            "Sec-Fetch-Site": "cross-site",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Dest": "empty",
            "Referer": "https://servicewechat.com/wxcfc12af652a2c540/322/page-frame.html",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "close"
        }
        # 定义请求体（这里是你的JSON数据）
        data = {
            "appid": "wxcfc12af652a2c540",
            "basicInfo": {
                "vid": 6001091846547,
                "vidType": 2,
                "bosId": 4000289554547,
                "productId": 146,
                "productInstanceId": 6440846547,
                "productVersionId": "14026",
                "merchantId": 2000130382547,
                "tcode": "weimob",
                "cid": 382721547
            },
            "extendInfo": {
                "wxTemplateId": 7517,
                "analysis": [],
                "bosTemplateId": 1000001403,
                "childTemplateIds": [
                    {"customId": 90004, "version": "crm@0.1.11"},
                    {"customId": 90002, "version": "ec@42.3"},
                    {"customId": 90006, "version": "hudong@0.0.201"},
                    {"customId": 90008, "version": "cms@0.0.419"}
                ],
                "quickdeliver": {"enable": False},
                "youshu": {"enable": False},
                "source": 1,
                "channelsource": 5,
                "refer": "onecrm-coupon-list",
                "mpScene": 1256
            },
            "queryParameter": {"status": 1},
            "i18n": {"language": "zh", "timezone": "8"},
            "pid": "100000559348",
            "storeId": "0",
            "pageNum": 1,
            "pageSize": 20
        }

        # 发送POST请求
        response = requests.post(url, headers=headers, json=data).json()
        if response['errcode'] == '0':
            pageList = response['data']['pageList']
            for i in range(len(pageList)):
                data = pageList[i]
                self.print_(
                    f"优惠名称：{data['name']}，使用描述:{data['explain']},有效期：{data['validDate']['validDateDesc']}"
                )
        else:
            self.print_(response['errmsg'])

    def get_my_point(self):
        # 定义请求的URL
        url = "https://xapi.weimob.com/api3/onecrm/point/myPoint/get"

        headers = {
            'Host': 'xapi.weimob.com',
            'Content-Length': '797',
            "Cookie": "rprm_cuid=7896440526qme0vege0o",
            'X-Apm-Parent-Page-Id': '151bfd94-2bac-4130-e694-47e268a6a7',
            'X-Wmsdk-Close-Store': 'v2',
            'X-Component-Is': 'onecrm/user-point-detail',
            'Weimob-Pid': '100000559348',
            'Parentrpcid': '49bda4f6ed357a6c',
            'X-Page-Route': 'onecrm/user-point-detail',
            'X-Cmssdk-Vidticket': '18046-1707896542.253-saas-w1-1400-20752306195',
            'Wos-X-Channel': '0:TITAN',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x6309092b) XWEB/8555',
            'Content-Type': 'application/json',
            'X-Wx-Token': '8606ca2c58138adccd2c4b7b61c42ad4b8ec0a5e1cda1bb598b53624b666139a44c1c72a3100c4e1b1f433f8b2268b95',
            'X-Wmsdk-Bc': '1 1707896440541',
            'Weimob-Bosid': '4000289554547',
            'X-Cms-Sdk-Request': '1.5.21',
            'X-Apm-Page-Id': 'faa48c1d-7a60-d4bc-4a51-26542efa9b',
            'X-Apm-Conversation-Id': '3f432809-e0a7-d5bb-ceb3-d475ac37fa',
            'X-Req-From': 'onecrm',
            'Xweb_xhr': '1',
            'X-Wmsdk-Vid': '6001091846547',
            'X-Biz-Id': '146',
            'Accept': '*/*',
            'Sec-Fetch-Site': 'cross-site',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://servicewechat.com/wxcfc12af652a2c540/322/page-frame.html',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'close',
        }
        # 定义请求体（这里是你的JSON数据）
        data = {"appid": "wxcfc12af652a2c540",
                "basicInfo": {"vid": 6001091846547, "vidType": 2, "bosId": 4000289554547, "productId": 146,
                              "productInstanceId": 6440846547, "productVersionId": "14026", "merchantId": 2000130382547,
                              "tcode": "weimob", "cid": 382721547},
                "extendInfo": {"wxTemplateId": 7517, "analysis": [], "bosTemplateId": 1000001403,
                               "childTemplateIds": [{"customId": 90004, "version": "crm@0.1.11"},
                                                    {"customId": 90002, "version": "ec@42.3"},
                                                    {"customId": 90006, "version": "hudong@0.0.201"},
                                                    {"customId": 90008, "version": "cms@0.0.419"}],
                               "quickdeliver": {"enable": False}, "youshu": {"enable": False}, "source": 1,
                               "channelsource": 5, "refer": "onecrm-user-point-detail", "mpScene": 1256},
                "queryParameter": None, "i18n": {"language": "zh", "timezone": "8"}, "pid": "100000559348",
                "storeId": "0", "request": {"isNeedRecordDisplay": False, "isQueryAllAccount": False}}

        # 发送POST请求
        response = requests.post(url, headers=headers, json=data).json()
        if response['errcode'] == '0':
            data = response['data']
            self.print_(f'当前账号有效积分=[{data["availablePoint"]}]')
        else:
            self.print_(response['errmsg'])

    def sign(self):
        # 定义请求的URL
        url = "https://xapi.weimob.com/api3/onecrm/mactivity/sign/misc/sign/activity/core/c/sign"
        headers = {
            'Host': 'xapi.weimob.com',
            'Cookie': 'rprm_cuid=7896440526qme0vege0o',
            'Content-Length': '768',
            'X-Apm-Parent-Page-Id': 'ab779a22-29c4-67bb-3dd2-ab14003670',
            'X-Wmsdk-Close-Store': 'v2',
            'X-Component-Is': 'onecrm/signgift',
            'Weimob-Pid': '100000559348',
            'Parentrpcid': '524909d286aa2103',
            'X-Page-Route': 'onecrm/signgift',
            'X-Cmssdk-Vidticket': '18046-1707896542.253-saas-w1-1400-20752306195',
            'Wos-X-Channel': '0:TITAN',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x6309092b) XWEB/8555',
            'Content-Type': 'application/json',
            'X-Wx-Token': '8606ca2c58138adccd2c4b7b61c42ad4b8ec0a5e1cda1bb598b53624b666139a44c1c72a3100c4e1b1f433f8b2268b95',
            'X-Wmsdk-Bc': '1 1707896440541',
            'Weimob-Bosid': '4000289554547',
            'X-Cms-Sdk-Request': '1.5.21',
            'X-Apm-Page-Id': '7dbafc5a-4ced-2591-e84f-88ec19cd11',
            'X-Apm-Conversation-Id': '3f432809-e0a7-d5bb-ceb3-d475ac37fa',
            'X-Req-From': 'onecrm',
            'Xweb_xhr': '1',
            'X-Wmsdk-Vid': '6001091846547',
            'X-Biz-Id': '146',
            'Accept': '*/*',
            'Sec-Fetch-Site': 'cross-site',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://servicewechat.com/wxcfc12af652a2c540/322/page-frame.html',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'close'
        }

        data = {"appid": "wxcfc12af652a2c540",
                "basicInfo": {"vid": 6001091846547, "vidType": 2, "bosId": 4000289554547, "productId": 146,
                              "productInstanceId": 6440846547, "productVersionId": "14026", "merchantId": 2000130382547,
                              "tcode": "weimob", "cid": 382721547},
                "extendInfo": {"wxTemplateId": 7517, "analysis": [], "bosTemplateId": 1000001403,
                               "childTemplateIds": [{"customId": 90004, "version": "crm@0.1.11"},
                                                    {"customId": 90002, "version": "ec@42.3"},
                                                    {"customId": 90006, "version": "hudong@0.0.201"},
                                                    {"customId": 90008, "version": "cms@0.0.419"}],
                               "quickdeliver": {"enable": False}, "youshu": {"enable": False}, "source": 1,
                               "channelsource": 5, "refer": "onecrm-signgift", "mpScene": 1256}, "queryParameter": None,
                "i18n": {"language": "zh", "timezone": "8"}, "pid": "100000559348", "storeId": "0",
                "customInfo": {"source": 0, "wid": 10266237815}
                }

        response = requests.post(url, headers=headers, json=data).json()
        self.print_(response)
        if response['errcode'] == '0':
            self.print_('今天签到成功')
            self.print_(response['data']['fixedReward'])
            self.print_(response['data']['extraReward'])
        else:
            self.print_(response['errmsg'])

    def do_task(self):
        # 获取当前领取的优惠卷信息
        self.get_my_coupon()
        # 获取我的积分信息
        self.get_my_point()
        # 执行签到
        self.sign()
        self.print_('')
        return


QI_HCK_TOKEN = '8606ca2c58138adccd2c4b7b61c42ad4b8ec0a5e1cda1bb598b53624b666139a44c1c72a3100c4e1b1f433f8b2268b95'
if 'QI_HCK_TOKEN' in os.environ:
    QI_HCK_TOKEN = os.environ.get("QI_HCK_TOKEN")
    print('获取到环境变量QI_HCK_TOKEN=' + QI_HCK_TOKEN)
if __name__ == '__main__':
    # 多个用@分隔
    token_list = QI_HCK_TOKEN.split('@')
    print_list = []
    for i in range(len(token_list)):
        task = QiHckTask(token_list[i])
        print_list.extend(task.run())
    send("七号仓库", print_list)
